package com.example.hotel_lxy.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.FoodDialogBean;
import com.example.hotel_lxy.bean.FoodInfoDialogBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.ui.activity.LoginActivity;
import com.example.hotel_lxy.utils.SPUtils;
import com.example.hotel_lxy.view.SaveBitmap;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.DisplayUtils;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.text.SimpleDateFormat;
import java.util.Date;

import okhttp3.Call;


/**
* 已选菜品
* create by Sincerly on 9999/9/9 0009
**/
public class SelectedInfoFoodDialog extends Dialog {
    private Context mContext;
    private static  String id;
    private RecyclerView rv_food;
    private TextView text_money;
    private TextView b_name;
    private TextView desk;
    private TextView prepare_desk;
    private TextView book;
    private TextView begin;
    private TextView director;
    private TextView top,name;
    private LinearLayout line1;
    private static String type; //是否是会议
    private static String time;//开始时间

    private Handler mHandler = new Handler();
    public SelectedInfoFoodDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    private View init() {
        String string = SPUtils.get(getContext(), "hotel", "").toString();
        View view = View.inflate(mContext, R.layout.dialog_selectedinfofood, null);
        rv_food=view.findViewById(R.id.rv_food);
        top=view.findViewById(R.id.top);
        name=view.findViewById(R.id.name);
        top.setText(string+"预点菜单");
        name.setText(string+"欢迎您");

        text_money=view.findViewById(R.id.text_money);
        b_name=view.findViewById(R.id.b_name);
        desk=view.findViewById(R.id.desk);
        prepare_desk=view.findViewById(R.id.prepare_desk);
        book=view.findViewById(R.id.book);
        begin=view.findViewById(R.id.begin);
        director=view.findViewById(R.id.director);
        line1=view.findViewById(R.id.line1);
        showDataFood();

        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                line1.setDrawingCacheEnabled(true);
                line1.buildDrawingCache();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Bitmap bmp = line1.getDrawingCache();
                        // 要在运行在子线程中
                        ShareDialog.show(mContext,null,"","2",bmp,"").setListener(new ShareDialog.OnDialogClickListener() {
                            @Override
                            public void onPositiveClick(String s) {
                                if(s.equals("2")){
                                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                                    Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                                    String str  = formatter.format(curDate);
                                    String time = getTime(str);
                                    boolean b = SaveBitmap.SaveBitmap(bmp, time + ".jpg", mContext);
                                    if(b){
                                        ToastUtils.shortToast(mContext,"保存成功");
                                        dismiss();
                                    }
                                    line1.destroyDrawingCache(); // 保存过后释放资源
                                }
                            }
                        });
                    }
                },100);
                return false;
            }
        });

        return view;
    }
    //字符串转时间戳
    public static String getTime(String timeString){
        String timeStamp = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm");
        Date d;
        try{
            d = sdf.parse(timeString);
            long l = d.getTime();
            timeStamp = String.valueOf(l);
        } catch(Exception e){
            e.printStackTrace();
        }
        return timeStamp;
    }
    private void showDataFood() {

        OkHttpUtils.post()
                .url(Api.POST_SCHEDULED_DISH_SET)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("id", ""+id)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "已选菜单onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "已选菜单onResponse   " + response);
                        BeanBean beanBean=JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("0")) {
                            FoodInfoDialogBean foodInfoDialogBean = JsonUtils.parseByGson(response, FoodInfoDialogBean.class);
                            FoodInfoDialogBean.DataBeanX.DataBean data = foodInfoDialogBean.getData().getData();
                            b_name.setText("宴会厅："+data.getB_name());

                            if(type.equals("1")){
                                desk.setText("人数："+data.getDesk()+"人");
                                begin.setText(time.replace("【宴会时间】","会议时间："));
                            }else{
                                desk.setText("桌数："+data.getDesk()+"桌");
                                begin.setText("宴会时间："+data.getBegin());
                            }
                            if(data.getPrepare_desk()!=0){
                                prepare_desk.setText(" 备"+data.getPrepare_desk()+"桌");
                            }else{
                                prepare_desk.setVisibility(View.INVISIBLE);
                            }

                            book.setText("预定人："+data.getBook());

                            text_money.setText("餐标："+data.getD_money());
                            director.setText("客户经理："+data.getDirector()+" "+data.getDirector_mobile());
                            rv_food.setLayoutManager(new LinearLayoutManager(mContext));
                            RBaseAdapter<FoodInfoDialogBean.DataBeanX.NewArraysBean> rBaseAdapter=new RBaseAdapter<FoodInfoDialogBean.DataBeanX.NewArraysBean>(mContext,
                                    R.layout.item_food, foodInfoDialogBean.getData().getNewArrays()) {
                                @Override
                                protected void fillItem(RViewHolder holder, FoodInfoDialogBean.DataBeanX.NewArraysBean item, int position) {
                                    TextView text_name=holder.itemView.findViewById(R.id.text_name);
                                    text_name.setText(item.getName()+":");
                                    RecyclerView rv_food_item=holder.itemView.findViewById(R.id.rv_food_item);
                                    rv_food_item.setLayoutManager(new LinearLayoutManager(mContext));
                                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            line1.setDrawingCacheEnabled(true);
                                            line1.buildDrawingCache();
                                            mHandler.postDelayed(new Runnable() {
                                                @Override
                                                public void run() {
                                                    // 要在运行在子线程中
                                                    final Bitmap bmp = line1.getDrawingCache();
                                                    ShareDialog.show(mContext,null,"","2",bmp,"").setListener(new ShareDialog.OnDialogClickListener() {
                                                        @Override
                                                        public void onPositiveClick(String s) {
                                                            if(s.equals("2")){
                                                                SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                                                                Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                                                                String str  = formatter.format(curDate);
                                                                String time = getTime(str);
                                                                boolean b = SaveBitmap.SaveBitmap(bmp, time + ".jpg", mContext);
                                                                if(b){
                                                                    ToastUtils.shortToast(mContext,"保存成功");
                                                                    dismiss();
                                                                }
                                                                line1.destroyDrawingCache(); // 保存过后释放资源
                                                            }
                                                        }
                                                    });
                                                }
                                            },100);
                                        }
                                    });
                                    RBaseAdapter<FoodInfoDialogBean.DataBeanX.NewArraysBean.DatasBean> rBaseAdapter1=new RBaseAdapter<FoodInfoDialogBean.DataBeanX.NewArraysBean.DatasBean>(mContext,
                                            R.layout.item_item_food,item.getDatas()) {
                                        @Override
                                        protected void fillItem(RViewHolder holder, FoodInfoDialogBean.DataBeanX.NewArraysBean.DatasBean item, int position) {
                                            TextView name=holder.itemView.findViewById(R.id.name);
                                            TextView num=holder.itemView.findViewById(R.id.num);
                                            TextView money=holder.itemView.findViewById(R.id.money);
                                            name.setText(""+item.getName());
                                            num.setText(""+item.getNum());
                                            money.setText("￥ "+item.getMoney());
                                            holder.itemView.setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    line1.setDrawingCacheEnabled(true);
                                                    line1.buildDrawingCache();
                                                    mHandler.postDelayed(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            // 要在运行在子线程中
                                                            final Bitmap bmp = line1.getDrawingCache();
                                                            ShareDialog.show(mContext,null,"","2",bmp,"").setListener(new ShareDialog.OnDialogClickListener() {
                                                                @Override
                                                                public void onPositiveClick(String s) {
                                                                    if(s.equals("2")){
                                                                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                                                                        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                                                                        String str  = formatter.format(curDate);
                                                                        String time = getTime(str);
                                                                        boolean b = SaveBitmap.SaveBitmap(bmp, time + ".jpg", mContext);
                                                                        if(b){
                                                                            ToastUtils.shortToast(mContext,"保存成功");
                                                                            dismiss();
                                                                        }
                                                                        line1.destroyDrawingCache(); // 保存过后释放资源
                                                                    }
                                                                }
                                                            });

                                                        }
                                                    },100);
                                                }
                                            });
                                        }

                                        @Override
                                        protected int getViewType(FoodInfoDialogBean.DataBeanX.NewArraysBean.DatasBean item, int position) {
                                            return 0;
                                        }
                                    };
                                    rv_food_item.setAdapter(rBaseAdapter1);
                                }

                                @Override
                                protected int getViewType(FoodInfoDialogBean.DataBeanX.NewArraysBean item, int position) {
                                    return 0;
                                }
                            };
                            rv_food.setAdapter(rBaseAdapter);

                        }
                        else if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                            return;
                        }

                    }
                });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
        setContentView(init());
    }

    public void showDialog() {
        if (!isShowing()) {
            show();
            WindowManager.LayoutParams lp = getWindow().getAttributes();
//            lp.height = DisplayUtils.getDisplayWidth(mContext) * 5 / 5;
            lp.width = DisplayUtils.getDisplayWidth(mContext) * 8 / 10;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setAttributes(lp);
            getWindow().setGravity(Gravity.CENTER);
        }
    }


    public static SelectedInfoFoodDialog show(Context context, OnDialogClickListener listener, String ids,String types,String times) {
        SelectedInfoFoodDialog dialog = new SelectedInfoFoodDialog(context, R.style.BottomDialogStyle);
        dialog.setListener(listener);
        id=ids;
        type=types;
       time=times;

        dialog.showDialog();
        return dialog;
    }

    public OnDialogClickListener listener;

    public SelectedInfoFoodDialog setListener(OnDialogClickListener listener) {
        this.listener = listener;
        return this;
    }
    public interface OnDialogClickListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(String s);
    }

}